package com.example.myFirstWebmagic.waterReport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.example.myFirstWebmagic.pipeline.ConsolePipelineDemo;
import com.example.myFirstWebmagic.util.StringUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 
 * ClassName: waterProcessor 
 * @Description: 获取国家地表水，水质量自动检测实时数据
 * @author xiongyao
 * @date 2017年3月6日
 */
public class WaterProcessor implements PageProcessor{

	// 部分一：抓取网站的相关配置，包括编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(10).setSleepTime(5000);
	
    private List<EnWaterStationReport> reports=new ArrayList<EnWaterStationReport>();
    
    
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://58.68.130.147"};
		// TODO Auto-generated method stub
		Spider.create(new WaterProcessor())
        //从"https://github.com/code4craft"开始抓
        .addUrl(startUrl)
        .addPipeline(new ConsolePipelineDemo())
        //开启5个线程抓取
        .thread(5)
        //启动爬虫
        .run();
	}
    
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		//流域的名称
		List<String> basinsName=page.getHtml().xpath("//area[@shape='rect']/@alt").all();
		//流域的ID
		List<String> basinsIdString=page.getHtml().xpath("//area[@shape='rect']/@onmousedown").all();
		Map<String,String> basins=new HashMap<String, String>();
		for(String basinsId:basinsIdString){
			
			String id=StringUtil.replaceNoNum(basinsId);
			basins.put(id, basinsName.get(basinsIdString.indexOf(basinsId)));
		}
		
		//监测点对应的流域ID
		List<String> stationBasinId=page.getHtml().xpath("//area[@shape='circle']/@onmousedown").all();
		List<String> stations=page.getHtml().xpath("//area[@shape='circle']/@alt").all();
		//存放监测点对应的流域
		Map<String,String> stationBasin=new HashMap<String, String>();
		for(String station:stations){
			int index=stations.indexOf(station);
			String[] tempString= stationBasinId.get(index).split(",");
			String basinId=StringUtil.replaceNoNum(tempString[tempString.length-1]);
			stationBasin.put(station, basins.get(basinId));
		}
		
		
		//监测点水质信息
		String stainfoString=page.getHtml().xpath("//input[@name='stainfo']/@value").toString();
		String[] stainfoAll=stainfoString.split("\\$!!");
		
		Map<String, EnWaterStationReport> waterStationMap=new HashMap<String, EnWaterStationReport>();
		
		for(String stainfos:stainfoAll){
			String[] stainfo= stainfos.split("\\$");
			EnWaterStationReport waterStationReport=new EnWaterStationReport();
			WaterStationPK stationPK=new WaterStationPK();
			//设置水质监测报告的各个属性
			stationPK.setTime(stainfo[1]);//测量时间小时
			waterStationReport.setPhValue(stainfo[2]);//PH值
			waterStationReport.setPhLevel(stainfo[3]);//PH值对应水质级别
			waterStationReport.setDissolvedOxygen(stainfo[4]);//溶解氧
			waterStationReport.setDissolvedOxygenLevel(stainfo[5]);//溶解氧水质级别
			waterStationReport.setPermanganateIndex(stainfo[6]);//高锰酸盐指数
			waterStationReport.setPermanganateIndexLevel(stainfo[7]);//高锰酸盐水质级别
			waterStationReport.setToc(stainfo[8]);//总有机碳指数
			waterStationReport.setTocLevel(stainfo[9]);//总有机碳水质级别
			waterStationReport.setAmmoniaOxygen(stainfo[10]);//氨氧指数
			waterStationReport.setAmmoniaOxygenLevel(stainfo[11]);//氨氧水质级别
			stationPK.setDate(stainfo[12]);//测量日期
			waterStationReport.setId(stationPK);
			waterStationMap.put(stainfo[0], waterStationReport);
		}
		//监测点名称和ID对应关系
		String stanameString=page.getHtml().xpath("//input[@name='staname']/@value").toString();
		String[] stanamesAll=stanameString.split("\\!!");
		//设置监测点的名称
		for(String stanames:stanamesAll){
			if(waterStationMap.containsKey(stanames.split("\\$")[0])){
				WaterStationPK waterStationPK=waterStationMap.get(stanames.split("\\$")[0]).getId();
				waterStationPK.setStationName(stanames.split("\\$")[1]);
				EnWaterStationReport waterStationReport=waterStationMap.get(stanames.split("\\$")[0]);
				waterStationReport.setId(waterStationPK);
				waterStationMap.put(stanames.split("\\$")[0], waterStationReport);
			}else{
				System.out.println(stanames);
			}
		}
		
		//断面属性
		String sectionPropertieString=page.getHtml().xpath("//input[@name='stainfodby']/@value").toString();
		String[] sectionPropertiesAll=sectionPropertieString.split("\\!!");
		//设置监测点的断面属性
		for(String sectionProperties:sectionPropertiesAll){
			if(sectionProperties.split("\\$").length==2){
				String id= sectionProperties.split("\\$")[0];
				String value=sectionProperties.split("\\$")[1];
				if(waterStationMap.containsKey(id)){
					
					EnWaterStationReport waterStationReport=waterStationMap.get(id);
					waterStationReport.setSectionProperties(value);
					waterStationMap.put(id, waterStationReport);
				}
			}
			
		}
		
		//监测点状态
		String stastatusString=page.getHtml().xpath("//*[@id='form1']/input[5]/@value").toString()+page.getHtml().xpath("//*[@id='form1']/input[6]/@value").toString();
		String[] stastatusAll=stastatusString.split("\\!!");
		Map<String, String> stastatusMap=new HashMap<String, String>();
		//设置监测点的状态
		for(String statusGroup:stastatusAll){
			String[] statu=statusGroup.split("\\$");
			String id="";
			String status="";
			id=statu[0];
			if(statu.length==2){
				status=statu[1];
			}
			stastatusMap.put(id, status);
		}
		
		for(Entry<String, String> entry: stastatusMap.entrySet()){
			if(waterStationMap.containsKey(entry.getKey())){
				EnWaterStationReport waterStationReport=waterStationMap.get(entry.getKey());
				waterStationReport.setStationStatus(entry.getValue());//设置监测点的状态
				waterStationReport.setBasin(stationBasin.get(waterStationReport.getId().getStationName()));
				waterStationMap.put(entry.getKey(), waterStationReport);
				
			}
		
		}
		
		for(EnWaterStationReport waterStationReport:waterStationMap.values()){
			if(StringUtil.isNotEmpty(waterStationReport.getId().getStationName())){
				reports.add(waterStationReport);
			}
		}

		page.putField("objectList", reports);
		
		
		
	}

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return this.site;
	}

}
